import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.text.DecimalFormat;

/**
 * 读取表格
 */
public class POIDemo2 {


    @Test
    public void fun() throws Exception {

        //加载xlsx文件

        Workbook workbook = new XSSFWorkbook("D:\\employee.xlsx");

        //通过工作簿找到sheet  

        Sheet sheet = workbook.getSheetAt(0);
        //获取一共多少行
        int maxRow = sheet.getLastRowNum();

        for (int i = 0; i <= maxRow; i++) {
            //获得具体每一行 
            Row row = sheet.getRow(i);
            //获取这一行 有多少个单元格 
            int lastCellNum = row.getLastCellNum();

            StringBuffer buffer = new StringBuffer();
            //遍历这一行的所有单元格
            for (int j = 0; j <= lastCellNum; j++) {
                //获取单元格
                Cell cell = row.getCell(j);
                if (cell != null) {
                    Object value = getValue(cell);
                    buffer.append(value).append("--");
                }

            }

            System.out.println(buffer.toString());

        }


    }

    public Object getValue(Cell cell) {
        CellType cellTypeEnum = cell.getCellTypeEnum();
        Object obj = null;
        switch (cellTypeEnum) {
            case STRING://字符串类型
                obj = cell.getStringCellValue();
                break;
            case BOOLEAN:// boolean类型
                obj = cell.getBooleanCellValue();
                break;
            case NUMERIC://数字和日期类型
                if (DateUtil.isCellDateFormatted(cell)) {
                    //如果是日期类型
                    obj = cell.getDateCellValue();
                } else {
                    //数字类型
                    obj = cell.getNumericCellValue();
                }
                break;
            case FORMULA://如果是公式
                obj = cell.getCellFormula();
                break;

        }
        return obj;


    }

}